package hikari.objects3d.light;

import com.gooddies.wiring.Wiring;
import hikari.managers.EngineProperties;
import hikari.managers.Manager3D;
import hikari.math.CircleDrawer;
import hikari.math.CylCoordResolver;
import hikari.utils.Utils;
import org.swig.jirr.E_LIGHT_TYPE;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.ITexture;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
public class DirectionalLight3dObject extends Light3dObject {
    private static ITexture billboardTexture;

    public DirectionalLight3dObject(ISceneManager sceneManager) {
        super(sceneManager, E_LIGHT_TYPE.ELT_DIRECTIONAL, "DirectionalLight");
    }

    @Override
    public void drawGizmos() {
        IVideoDriver driver = Wiring.getComponent(Manager3D.class).getVideoDriver();
        driver.resetTransformMatrix();
        driver.setMaterial(Wiring.getComponent(EngineProperties.class).getSMaterialProperty("lightGizmoMaterialFront"));
        vector3df normal = getMeshSceneNode().getRotation().rotationToDirection();
        CircleDrawer circle = new CircleDrawer(20, getMeshSceneNode().getAbsolutePosition(), normal, 30);
        circle.drawCircle();

        vector3df arrowStart = normal.addOperator(getMeshSceneNode().getAbsolutePosition());
        vector3df arrowEnd = normal.timesOperator(20).addOperator(getMeshSceneNode().getAbsolutePosition());
        driver.draw3DLine(arrowStart, arrowEnd);
        CylCoordResolver resolver = new CylCoordResolver(arrowEnd, normal);
        for (int i = 0; i < 4; i++) {
            vector3df point = resolver.resolve((float) Math.toRadians(90 * i), 2, -2);
            driver.draw3DLine(point, arrowEnd);
        }
    }

    @Override
    public String objectType() {
        return "directionalLight";
    }

    @Override
    protected ITexture getBillboardTexture() {
        if (billboardTexture == null) {
            billboardTexture = Wiring.getComponent(Utils.class).loadTextureFromResource("icons/omniLight.png");
        }
        return billboardTexture;
    }
}
